import { PeriodTaskApiService } from 'src/app/api/period-task-api.service';
import { PeriodicTask } from './../../model/task';
import { Injectable } from '@angular/core';
import { Resolve,
  RouterStateSnapshot,
  ActivatedRouteSnapshot
} from '@angular/router';
import { Observable,EMPTY} from 'rxjs';
import { map, take } from 'rxjs/operators';

@Injectable()
export class PeriodicTaskDetailResolver implements Resolve<PeriodicTask> {
  constructor(
    private taskApi: PeriodTaskApiService,
  ) { }
  resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): PeriodicTask | Observable<PeriodicTask> | Promise<PeriodicTask> {
    const id = route.paramMap.get('id');
    if (id) {
      return this.taskApi.fetchItem(parseInt(id, 10)).pipe(
        map(item => new PeriodicTask(item))
      );
    }
    return EMPTY;
  }

}
